// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import AutomaticRecode from "../../Commons/AutomaticRecode";
import UIManager from "../../Managers/UIManager";
import EquipManager from "./EquipManager";
import EquipTemplate from "./EquipTemplate";

export default class EquipEntity extends AutomaticRecode {

    public id: number;
    public level: number;
    public isWear: boolean;
    public exp: number;

    constructor(value: any) {
        super();
        this.init(value);
        this.exp = value.exp == null ? 0 : value.exp;
    }

    get equipTp(): EquipTemplate {
        return EquipManager.single.getEquipTemplate(this.id);
    }

    get attributeDes(): string {
        var tp = EquipManager.single.getAttributeTemplate(this.equipTp.attributeId0);
        var str = `${tp.attributeName} +${this.attributeValue}`;
        if (this.equipTp.attributeId0 == 102 || this.equipTp.attributeId0 == 103 || this.equipTp.attributeId0 == 105) {
            str += "%";
        }
        return str;
    }

    get attributeValue(): number {
        return (this.level - 1) * this.equipTp.upgradeGrow + this.equipTp.attributeValue0;
    }

    get upgradeExp(): number {
        return this.level * this.level + this.level * 3;
    }
    get expValue(): number {
        return this.equipTp.exp;
    }

    getExp(_exp: number) {
        this.exp += _exp;
        if (this.exp < this.upgradeExp) {
            UIManager.single.showTips("装备<@1>经验 +@2。".replace("@1", this.equipTp.equipName).replace("@2", _exp.toString()));
        }
        while (this.exp >= this.upgradeExp) {
            this.exp -= this.upgradeExp;
            this.level++;
            UIManager.single.showTips("装备<@>等级提升。".replace("@", this.equipTp.equipName));
        }
    }
}
